ESP32 I2C Communication: Set Pins, Multiple Bus Interfaces and Peripherals 您所在的位置:网站首页 esp32 gpio arduino ESP32 I2C Communication: Set Pins, Multiple Bus Interfaces and Peripherals

ESP32 I2C Communication: Set Pins, Multiple Bus Interfaces and Peripherals

2022-12-16 15:45| 来源: 网络整理| 查看: 265

The ESP32 has two I2C bus interfaces that can serve as I2C master or slave. In this tutorial we’ll take a look at the I2C communication protocol with the ESP32 using Arduino IDE: how to choose I2C pins, connect multiple I2C devices to the same bus and how to use the two I2C bus interfaces.

ESP32 I2C Communication: Set Pins, Multiple Bus Interfaces and Peripherals (Arduino IDE)

In this tutorial, we’ll cover the following concepts:

Connecting I2C Devices with ESP32Scan I2C Address with ESP32Use Different I2C Pins with ESP32 (change default I2C pins)ESP32 with Multiple I2C Devicessame bus, different addressessame addressESP32 Using Two I2C Bus Interfaces

We’ll program the ESP32 using Arduino IDE, so before proceeding with this tutorial you should have the ESP32 add-on installed in your Arduino IDE. Follow the next tutorial to install the ESP32 on the Arduino IDE, if you haven’t already.

Installing the ESP32 Board in Arduino IDE (Windows, Mac OS X, and Linux instructions) Introducing ESP32 I2C Communication Protocol

I²C means Inter Integrated Circuit (it’s pronounced I-squared-C), and it is a synchronous, multi-master, multi-slave communication protocol. You can connect :

multiple slaves to one master: for example, your ESP32 reads from a BME280 sensor using I2C and writes the sensor readings in an I2C OLED display.multiple masters controlling the same slave: for example, two ESP32 boards writing data to the same I2C OLED display.

We use this protocol many times with the ESP32 to communicate with external devices like sensors and displays. In these cases, the ESP32 is the master chip and the external devices are the slaves.

I2C Communication protocol with ESP32 board using Arduino IDE multiple devices

We have several tutorials with the ESP32 interfacing with I2C devices:

0.96 inch I2C OLED display with ESP32ESP32 Built-in OLED BoardI2C LCD Display with ESP32BMP180 with ESP32BME280 with ESP32 ESP32 I2C Bus Interfaces

The ESP32 supports I2C communication through its two I2C bus interfaces that can serve as I2C master or slave, depending on the user’s configuration. Accordingly to the ESP32 datasheet, the I2C interfaces of the ESP32 supports:

Standard mode (100 Kbit/s) Fast mode (400 Kbit/s) Up to 5 MHz, yet constrained by SDA pull-up strength 7-bit/10-bit addressing mode Dual addressing mode. Users can program command registers to control I²C interfaces, so that they have more flexibility Connecting I2C Devices with ESP32

I2C communication protocol uses two wires to share information. One is used for the clock signal (SCL) and the other is used to send and receive data (SDA).

Note: in many breakout boards, the SDA line may also be labeled as SDI and the SCL line as SCK.

BME280 sensor I2C SDA (SDI) and SCL (SCK) pins

The SDA and SCL lines are active low, so they should be pulled up with resistors. Typical values are 4.7k Ohm for 5V devices and 2.4k Ohm for 3.3V devices.

Most sensors we use in our projects are breakout boards that already have the resistors built-in. So, usually, when you’re dealing with this type of electronics components you don’t need to worry about this.

Connecting an I2C device to an ESP32 is normally as simple as connecting GND to GND, SDA to SDA, SCL to SCL and a positive power supply to a peripheral, usually 3.3V (but it depends on the module you’re using).

I2C DeviceESP32SDASDA (default is GPIO 21)SCLSCL (default is GPIO 22) GNDGNDVCCusually 3.3V or 5V

When using the ESP32 with Arduino IDE, the default I2C pins are GPIO 22 (SCL) and GPIO 21 (SDA) but you can configure your code to use any other pins.

Recommended reading: ESP32 GPIO Reference Guide

Scan I2C Address with ESP32

With I2C communication, each slave on the bus has its own address, a hexadecimal number that allows the ESP32 to communicate with each device.

The I2C address can be usually found on the component’s datasheet. However, if it is difficult to find out, you may need to run an I2C scanner sketch to find out the I2C address.

You can use the following sketch to find your devices’ I2C address.

/********* Rui Santos Complete project details at https://randomnerdtutorials.com *********/ #include void setup() { Wire.begin(); Serial.begin(115200); Serial.println("\nI2C Scanner"); } void loop() { byte error, address; int nDevices; Serial.println("Scanning..."); nDevices = 0; for(address = 1; address < 127; address++ ) { Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有